package com.gxlu.ms.base.module.context;

import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author andy.lhc
 * @date 2018/3/16
 */
public final class RpcContext {

    private static final ThreadLocal<RpcContext> rpcThreadLocal = new ThreadLocal<RpcContext>();

    public static final String USER_ID = "uid" ;

    /**
     * 是否压测 1:压测，0:正常请求
     */
    public static final String PERSIST_TEST = "pt" ;

    long startTime;

    private Map<String, String> attributes = new TreeMap<>();

    String traceId;


    public RpcContext(String traceId){
        this.traceId = traceId ;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public void putAttribute(String key, String value){
        attributes.put(key,value);
    }

    public String getAttribute(String key){
        return attributes.get(key);
    }
    public Map<String, String> getAttributes() {
        return attributes;
    }

    public void removeAttribute(String key){
        if(!attributes.containsKey(key)){
            return ;
        }
        attributes.remove(key);
    }

    /**
     * 一定要清除，否则会有内存泄漏
     */
   /* public void remove(){
        rpcThreadLocal.remove();
    }*/

    public static RpcContext get(){
        return  rpcThreadLocal.get();
    }

    public static  void  set(RpcContext  rpcContext){
        rpcThreadLocal.set(rpcContext);
    }


}
